package com.sss.demo.servlet;

import com.sss.demo.entity.User;
import com.sss.demo.service.UserService;
import com.sss.demo.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

@WebServlet({"/jsp/login", "/jsp/register","/jsp/check"})
public class UserServlet extends HttpServlet {
    UserService service;
    String uri;

    /* service方法会将get/post都进行拦截 */
    //将service方法改名为doGet
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  获取请求的完整路径
        String uriStr = req.getRequestURI();
        ///demo_war_exploded/jsp/register
        ///demo_war_exploded/jsp/login
        uri = uriStr.substring(uriStr.lastIndexOf('/') + 1);


        System.out.println("请求的完整路径:" + uri);

        doPost(req, resp);  //调用本类的doPost方法
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        switch (uri) {
            case "login": login(req, resp);break;
            case "register": register(req, resp);break;
            case "check":check(req,resp);break;
            default: break;
        }
        /*if("login".equals(uri)){
            login(req, resp);
        }else if ("register".equals(uri)){
            // register
        }*/
    }

    private void check(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        User user=new User();
        System.out.println("========"+req.getParameter("name"));
        user.setName(req.getParameter("name"));
        service=new UserServiceImpl();
        boolean boo=service.selectOne(user);
        String str=" ";
        if(boo){
            str="用户已存在,请更改用户名";
        }else{
            str="用户名不存在，可以正常注册";
        }
        Writer out = null;
        try {
           out= resp.getWriter();   //响应对象  获取字符输入流
           out.write(str);
           out.flush();
           out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void register(HttpServletRequest req, HttpServletResponse resp) {
        //  处理页面拿过来的数据，添加进数据库
        User user = new User();
        user.setName(req.getParameter("name"));
        user.setPassword(req.getParameter("pwd"));
        service = new UserServiceImpl();
        boolean boo = service.register(user);
        String path = req.getContextPath();

        try {
            if (boo) {
                resp.sendRedirect(path + "/jsp/index.jsp");
            }else{
                    req.getSession().setAttribute("msg","注册失败");
                    resp.sendRedirect(path + "/jsp/fail.jsp");
                }
            } catch(IOException e){
                e.printStackTrace();
            }
        }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取用户的数据
        String userStr = req.getParameter("user");
        String passwordStr = req.getParameter("password");

        User user = new User();
        user.setName(userStr);
        user.setPassword(passwordStr);
        service = new UserServiceImpl();
        boolean boo = service.login(user);
        System.out.println("用户登录为：" + boo);

        String path = req.getContextPath();   //获取servlet容器的路径
        //System.out.println("获取servlet容器的路径"+path);
        if (boo) {
            resp.sendRedirect(path + "/index.jsp");
        } else{
            req.getSession().setAttribute("msg","登录失败");
            resp.sendRedirect(path + "/jsp/fail.jsp");
        }

    }
}
